<?php
/**
 * 汇友科技
 * @copyright (c) 2017 http://www.huiyou365.net All rights reserved.
 * @author: Creatint <creatint@163.com>
 */

namespace Admin\Model;

use Common\Model\Model;
use Think\Page;
use Common\Contract\Position as PositionContract;
use Common\Contract\Model as ModelContract;

class ArticleModel extends Model {
    protected $error = null;
    private $lists = null;
    private $page = null;
    protected $data = null;

    protected $_validate = array(
        array('name','/^[a-zA-Z]\w{0,39}$/','标识不合法',self::VALUE_VALIDATE,'regex',self::MODEL_BOTH),
        array('name','','标识已存在',self::VALUE_VALIDATE,'unique',self::MODEL_BOTH),
        array('title','require','名称不能为空',self::MUST_VALIDATE,'regex',self::MODEL_BOTH),
        array('title','3,50','名称长度在3~50之间',self::MUST_VALIDATE,'length',self::MODEL_BOTH),
        array('uid','require','用户ID不能为空',self::EXISTS_VALIDATE,'regex',self::MODEL_INSERT),
        array('content', '1,5000', '文章长度不能超过500个字符', self::VALUE_VALIDATE, 'length', self::MODEL_BOTH),
        array('status','0,1','状态不正确',self::VALUE_VALIDATE,'in',self::MODEL_BOTH),
    );

    protected $_auto = array(
        array('position','get_position',self::MODEL_BOTH,'callback'),
        array('updated_at',NOW_TIME,self::MODEL_BOTH),
        array('created_at',NOW_TIME,self::MODEL_INSERT)
    );



    public function updateArticle($data){
        $this->data = $data;

        $data = $this->create($this->data);
        if ( empty($data) ){
            return false;
        }
        if ( $data['id'] >0 ) {
            if ( $result = $this->save($data)){
                // 更新成功

            } else {
                // 更新失败
            }
        } else {
            if ( $result = $this->add($data)){
                // 新增成功
            } else {
                // 新增失败
            }
        }
        return $result;
    }

    public function position($pos = null, $limit = null, $field = null, $status = 1){

        dump($pos);
        // 查询条件
        $map = $this->map($status, $pos);

        // 设置列表数量
        is_numeric($limit) && $this->limit($limit);

        // 进行查询
        $this->lists = $this
            ->where($map)
            ->order('updated_at DESC')
            ->select();

        return $this;
    }

    public function map($status = 1, $pos = null){
        $map = array();

        // 状态
        $map['status']  =   $status;

        // 推荐位
        if ( $pos >0 )
            $map[]  =   "position & {$pos} = {$pos}";

        return $map;
    }

    public function next( $info )
    {
        $map = array(
            'status'    =>  1,
            'updated_at'  =>  array('lt', $info['updated_at'])
        );
        $res = $this->where($map)->find();

        return $res;
    }
    public function prev( $info )
    {
        $map = array(
            'status'    =>  1,
            'updated_at'  =>  array('gt', $info['updated_at'])
        );
        $res = $this->where($map)->find();
        return $res;
    }


    public function getLists(){
        return $this->lists;
    }

    public function getPage(){
        return $this->page;
    }

    public function getError(){
        return $this->error;
    }

    public function get_position($pos){
        if ($pos)
            $this->position = $pos;
        if ( !is_array($this->position) ){
            return 0;
        } else {
            $pos = 0;
            foreach ( $this->position as $k=>$v ){
                $pos += $v;
            }
            return $pos;
        }
    }

    public function remove($id){
        if ( !$id ) {
            $this->error = '待删除项为空';
            return false;
        }

        $map = [];

        if (is_array($id)) {
            foreach($id as $k=>$v){
                $map[$k] = $v;
            }
        } else {
            if (is_numeric($id)) {
                $id = (array)(int)$id;
            } else if ( is_string($id) && strpos($id, ',') > 0) {
                $id = preg_split('/[,，;\r\n]+/', trim($id, ",，;\r\n"));
            }

            $map = [
                'id'=>[
                    'IN',
                    $id
                ]
            ];
        }



        if ( !$id ) {
            $this->error = '待删除项为空';
            return false;
        }

        if ( $this->where($map)->setField('status', -1) ) {
            return true;
        } else {
            return false;
        }
    }
}
